package com.bw.controller;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.config.ExcelUtils;
import com.bw.config.Result;
import com.bw.domain.SysMenu;
import com.bw.domain.SysRole;
import com.bw.service.SysMenuService;
import com.bw.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/RoleManger")
public class RoleController {

    @Autowired
    SysRoleService sysRoleService;

    @RequestMapping("/listRole")
    public Result listRole(Page page,String roleName,String roleKey,String status,String beginTime,String endTime){
        IPage<SysRole> list=sysRoleService.listRole(page,roleName,roleKey,status,beginTime,endTime);
        return Result.ok(list);
    }
    //导出
    @RequestMapping("/exportExcel")
    public String exportExcel(){
        List<SysRole> list = sysRoleService.listExcel();
        String fileName="D:\\excel\\role.xlsx";
        EasyExcel.write(fileName,SysRole.class).sheet().doWrite(list);
        return "导出Excel成功";
    }

    @RequestMapping("/delRole")
    public Result del(Integer roleId){
         sysRoleService.removeById(roleId);
        return Result.ok();
    }

    @RequestMapping("/upstatus")
    public Result upstatus(Integer roleId){
        sysRoleService.upStatus(roleId);
        return Result.ok();
    }

    @RequestMapping("/listMenu")
    public Result listMenu(){
        return Result.ok();
    }

    @Autowired
    SysMenuService menuService;
    @RequestMapping("/listMenuTree")
    public Result listMenuTree(){
        List<SysMenu> list=menuService.list();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        //转换器
        List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getMenuName());
                    // 扩展属性 ...
                    tree.putExtra("label", treeNode.getMenuName());
                });
        return Result.ok(treeNodes);
    }

}
